#include "progressdlg.h"
#include <QProgressBar>
#include <QFont>
#include <QProgressDialog>

ProgressDlg::ProgressDlg(QWidget *parent)
    : QDialog(parent)
{
    QFont font("ZYSong18030",12);
    setFont(font);
    setWindowTitle(tr("Progress"));

    fileNum = new QLabel;
    fileNum->setText(tr("文件数目:"));
    fileNumLineEdit = new QLineEdit;
    fileNumLineEdit->setText(tr("100000"));

    progressType = new QLabel;
    progressType->setText(tr("显示类型:"));
    comboBox = new QComboBox;
    comboBox->addItem(tr("progressBar"));
    comboBox->addItem(tr("progressDialog"));

    progressBar = new QProgressBar;

    starBtn = new QPushButton;
    starBtn->setText(tr("开始"));

    mainLayout = new QGridLayout(this);
    mainLayout->addWidget(fileNum,0,0);
    mainLayout->addWidget(fileNumLineEdit,0,1);
    mainLayout->addWidget(progressType,1,0);
    mainLayout->addWidget(comboBox,1,1);
    mainLayout->addWidget(progressBar,2,0,1,7);
    mainLayout->addWidget(starBtn,3,1);
    mainLayout->setMargin(15);//窗体中部件的显示间隔
    mainLayout->setSpacing(10);//
    connect(starBtn,SIGNAL(clicked()),this,SLOT(startProgress()));
}

void ProgressDlg::startProgress()
{
    bool ok;
    int num = fileNumLineEdit->text().toInt(&ok);//获取当前文件数目

    if(comboBox->currentIndex() == 0)
    {
        progressBar->setRange(0,num);
        for(int i = 1;i < num + 1; i++)
        {
            progressBar->setValue(i);
        }
    }
    else if(comboBox->currentIndex() == 1)
    {
        QProgressDialog *progressDialog = new QProgressDialog(this);
        QFont font("ZYSong18030",12);
        progressDialog->setFont(font);
        progressDialog->setWindowModality(Qt::WindowModal);//设置进度对话框采用模态方式进行显示，即显示进度的同时，其它窗口将不响应输入信号
        progressDialog->setMinimumDuration(5);//设置进度对话框出现需等待的时间，此处设置为5秒
        progressDialog->setWindowTitle(tr("Please wait..."));//设置进度对话框的窗体标题
        progressDialog->setLabelText(tr("Copying..."));//设置进度对话框的显示文字信息
        progressDialog->setCancelButtonText(tr("Cancel"));//设置进度对话框的取消按钮的显示文字
        progressDialog->setRange(0,num);//设置进度对话框的步进范围
        for(int i = 1; i < num+1;i++)
        {
            progressDialog->setValue(i);
            if(progressDialog->wasCanceled())//检测取消按钮是否被触发
            {
                return;
            }
        }

    }
    
}

ProgressDlg::~ProgressDlg()
{

}
